using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy {
	public class NY_Future_Method_B : SignalObject {
		
		private IOrderMarket m_Buy_Order;
		private IOrderMarket m_Sell_Order;
		
		private IOrderPriced m_SellLimit_First;
        private IOrderPriced m_CoverLimit_First;
		
		private IOrderPriced m_SellLimit_Second;
        private IOrderPriced m_CoverLimit_Second;
		
        private RSI m_RSI;
        private VariableSeries<Double> m_myrsi;
		private ISeries<double> Price { get; set; }
		private double OverBought;
		
		[Input]
        public int Length { get; set; }

		[Input]
		public int Lost_Stop{get; set;}
		
		[Input]
		public int Profit_FirstLimit{get;set;}
		
		[Input]
		public int Profit_SecondLimit{get;set;}
		
		[Input]
        public double OverSold { get; set; }
				
		[Input]
        public int Order_Initial { get; set; }
		
		[Input]
        public int ExitQuantity_First { get; set; }
		
		[Input]
        public int ExitQuantity_Second { get; set; }
		
		[Input]
        public int Pct_Trailing { get; set; }
		
		
		public NY_Future_Method_B(object _ctx):base(_ctx){

		    Order_Initial = 3;
			ExitQuantity_First = 1;
 			ExitQuantity_Second = 1;
			
			Length = 55;
			OverSold = 20;
			OverBought = 100 - OverSold;
			Lost_Stop = 3;
			Profit_FirstLimit = 12;
			Profit_SecondLimit = 40;
			Pct_Trailing = 15;
		}
		
		protected override void Create() {
			// create variable objects, function objects, order objects etc.
			//SVN Test
			m_Buy_Order = OrderCreator.MarketNextBar(new SOrderParameters( Contracts.UserSpecified, EOrderAction.Buy));
			m_Sell_Order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified , EOrderAction.SellShort));
            
			m_SellLimit_First = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "Long Gain #1 ", EOrderAction.Sell, OrderExit.FromAll));
 			m_CoverLimit_First = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "Short Gain #1", EOrderAction.BuyToCover,OrderExit.FromAll));
			
			m_SellLimit_Second = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "Long Gain #2 ", EOrderAction.Sell, OrderExit.FromAll));
 			m_CoverLimit_Second = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "Short Gain #2", EOrderAction.BuyToCover,OrderExit.FromAll));
			
			m_RSI = new RSI(this);
			m_myrsi = new VariableSeries<Double>(this);
			
		}
		protected override void StartCalc() {
			// assign inputs 
			
			Price = Bars.Close;
			m_RSI.price = Price;
			m_RSI.length = Length;
		}
		protected override void CalcBar(){
			// strategy logic 
			TimeSpan Time_Start = new TimeSpan(9,0,0);
			TimeSpan Time_End =  new TimeSpan(16,0,0);
			
		    m_myrsi.Value = m_RSI[0];
			
          //  if ((Time_Start <= Bars.Time[0].TimeOfDay) && (Time_End >= Bars.Time[0].TimeOfDay) )
			//{
				
				//Lost Exit 
				if (StrategyInfo.MarketPosition != 0)
				{
					GenerateStopLoss(Lost_Stop * this.CurrentPosition.OpenLots );
					GeneratePercentTrailing(Profit_SecondLimit * this.CurrentPosition.OpenLots , Pct_Trailing);
				}
				
				
				
				if (Bars.CurrentBar > 1){
					
					  if (this.CrossesOver(m_myrsi,OverBought)){
	                    m_Buy_Order.Send(Order_Initial);
	                }
					
					if (this.CrossesUnder(m_myrsi,OverSold)){
						m_Sell_Order.Send(Order_Initial);
					}
					
					if (StrategyInfo.MarketPosition > 0)
					{
						m_SellLimit_First.Send(this.EntryPrice() + Profit_FirstLimit, ExitQuantity_First);
						m_SellLimit_Second.Send(this.EntryPrice() + Profit_SecondLimit, ExitQuantity_Second);
					}
					else if (StrategyInfo.MarketPosition < 0)
					{
						double Mark_Profit1 = this.EntryPrice() - Profit_FirstLimit;
						double Mark_Profit2 = this.EntryPrice() - Profit_SecondLimit;
						
						#if DEBUG
							Output.WriteLine("CurrentSell Mark 1  = {0}; DateTime = {1} ; EntryPrice = {2}", Mark_Profit1, DateTime.Now, this.EntryPrice() );
							Output.WriteLine("CurrentSell Mark 2  = {0}; DateTime = {1} ; EntryPrice = {2}", Mark_Profit2, DateTime.Now, this.EntryPrice() );
						#endif
						m_CoverLimit_First.Send(Mark_Profit1, ExitQuantity_First);
						m_CoverLimit_Second.Send(Mark_Profit2, ExitQuantity_Second);
					}
	            }
			//}
		}
	}
}